﻿using SHRIS.MIS.Common.Model;
using SHRIS.MIS.Common.Util.Extension;
using SHRIS.MIS.Common.Util.Model;
using SHRIS.MIS.Entity.SystemManage;
using SHRIS.MIS.IService.SystemManage;
using SHRIS.MIS.Model.Param.SystemManage;
using SHRIS.MIS.Service.Cache;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SHRIS.MIS.Business.SystemManage
{
    public class DataDictDetailBLL
    {
        private IDataDictDetailService _dataDictDetailService ;

        private DataDictDetailCache _dataDictDetailCache;

        public DataDictDetailBLL(IDataDictDetailService dataDictDetailService, DataDictDetailCache dataDictDetailCache)
        {
            _dataDictDetailService = dataDictDetailService;
            _dataDictDetailCache = dataDictDetailCache;
        }

        #region  获取数据
        public async Task<TEntity<List<DataDictDetailEntity>>> GetList(DataDictDetailListParam param)
        {
            TEntity<List<DataDictDetailEntity>> obj = new TEntity<List<DataDictDetailEntity>>();
            obj.Data = await _dataDictDetailService.GetList(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TEntity<List<DataDictDetailEntity>>> GetPageList(DataDictDetailListParam param, Pagination pagination)
        {
            TEntity<List<DataDictDetailEntity>> obj = new TEntity<List<DataDictDetailEntity>>();
            obj.Data = await _dataDictDetailService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TEntity<DataDictDetailEntity>> GetEntity(Guid id)
        {
            TEntity<DataDictDetailEntity> obj = new TEntity<DataDictDetailEntity>();
            obj.Data = await _dataDictDetailService.GetEntity(id);
            obj.Tag = 1;
            return obj;
        }

        public async Task<TEntity<int>> GetMaxSort()
        {
            TEntity<int> obj = new TEntity<int>();
            obj.Data = await _dataDictDetailService.GetMaxSort();
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 提交数据
        public async Task<TEntity<string>> SaveForm(DataDictDetailEntity entity)
        {
            TEntity<string> obj = new TEntity<string>();
            if (entity.DictKey <= 0)
            {
                obj.Message = "字典键必须大于0";
                return obj;
            }
            if (_dataDictDetailService.ExistDictKeyValue(entity))
            {
                obj.Message = "字典键或值已经存在！";
                return obj;
            }
            await _dataDictDetailService.SaveForm(entity);
            _dataDictDetailCache.Remove();
            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TEntity> DeleteForm(string ids)
        {
            TEntity<long> obj = new TEntity<long>();
            await _dataDictDetailService.DeleteForm(ids);
            _dataDictDetailCache.Remove();
            obj.Tag = 1;
            return obj;
        }
        #endregion
    }
}
